    //
//  EfengBeiZhuController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/1/26.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EfengBeiZhuController.h"
#import "JKAlertDialog.h"
#import "CHSCharacterCountTextView.h"
#import "CXmppMgr.h"
#import "CContactMgr.h"
#import "CXmppRequestMgr.h"
#import "UIUtil.h"
#import "MBProgressHUD+WZ.h"
#import "UIBarButtonItem+category.h"
#import "CGroupModel.h"
#import "CGroupMgr.h"


@interface EfengBeiZhuController ()<CHSCharacterCountTextViewDelegate>
{
    CHSCharacterCountTextView   *_textView;
    UIButton *_cleanButton;
}

StrongProperty CGroupMgr *groupMgr;
@end

@implementation EfengBeiZhuController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"修改备注名称"];
    
    self.groupMgr = [[CGroupMgr alloc] init];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"call_back" title:@"返回" target:self sel:@selector(back) titleFont:Font18 titleColor:WhiteColor];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:nil title:@"保存" target:self sel:@selector(save) titleFont:Font18 titleColor:WhiteColor];
    
    //    _textView = [[CHSCharacterCountTextView alloc] initWithFrame:CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH - 20, 140)];
    //    _textView.center = CGPointMake(HARDWARE_SCREEN_WIDTH/2, _textView.frame.size.height/2+10);
    //    [_textView setBackgroundColor:ClearColor];
    //    _textView.delegate = self;
    //    UIImageView* textBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _textView.frame.size.width+16, _textView.frame.size.height+10)];
    //    [textBG setCenter:_textView.center];
    //    CGRect fra = _textView.frame;
    //    [textBG setFrame:fra];
    //    [textBG setImage:[[UIImage imageNamed:@"searchcontentbg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch]];
    //    [self.view addSubview:textBG];
    _textView = [[CHSCharacterCountTextView alloc] init];
    _textView.layer.borderWidth = 0.5f;
    _textView.layer.borderColor = RGBCOLOR(0xd4, 0xd4, 0xd4).CGColor;
    _textView.layer.cornerRadius = 2.5f;
    _textView.delegate = self;
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.userInteractionEnabled = YES;
    _textView.placeHoldText = @"请输入讨论组备注";
    [self.view addSubview:_textView];
    
    if (self.group.GroupName.length > 0) {
        _textView.text = self.group.GroupName;
    }
    
    CGFloat padding = 10.0f;
    CGFloat hight = 200.0f;
    
    _textView.frame = CGRectMake(padding, padding, HARDWARE_SCREEN_WIDTH - 2 * padding, hight);
    
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    _cleanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cleanButton.frame = CGRectMake(HARDWARE_SCREEN_WIDTH - 40 -padding, hight+15, 40, 40);
    [_cleanButton setBackgroundImage:[UIImage imageNamed:@"deleteAccountImage"] forState:UIControlStateNormal];
    [_cleanButton setBackgroundImage:[UIImage imageNamed:@"deleteAccountHighLightImage"] forState:UIControlStateHighlighted];
    [_cleanButton addTarget:self action:@selector(cleanText) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_cleanButton];
}
-(void)cleanText
{
    _textView.text = @"";
}
#pragma mark CHSCharacterCountTextViewDelegate
//bug 8679 fixed by lixu
-(void)characterCountTextViewDidChanged:(CHSCharacterCountTextView *)textView
{
    if (textView.text.length == 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
    else
    {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
}
-(void)characterCountTextViewDidBeginEditing:(CHSCharacterCountTextView *)textView
{
    if (textView.text.length == 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
    else
    {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveSuccess:) name:K_XMPPUPDATEDISCUSSIONINFO object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveFailed) name:K_XMPPDIDFAILEDRECEIVEIQ object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDIDFAILEDRECEIVEIQ object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPUPDATEDISCUSSIONINFO object:nil];
}


#pragma mark 通知中心方法
- (void)saveSuccess:(NSNotification *)notice
{
    [UIUtil hideMBProgressViewForView:self.view];
    self.group.GroupName = _textView.text;
    [self.groupMgr updateGroup:self.group];
    [MBProgressHUD showSuccess:@"保存成功"];
    [self.navigationController popViewControllerAnimated:YES ];
    
}

- (void)saveFailed
{
    [UIUtil hideMBProgressViewForView:self.view];
    [MBProgressHUD showError:@"保存失败，请您检查网络"];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_textView endEditing:YES];
}
-(void)save
{
    [_textView endEditing:YES];
    if (_textView.text.length <= 0) {
        [MBProgressHUD showError:@"请输入备注名称"];
        return;
    }
    
    [self updateSignature];
}


-(void)back
{
    //bug 9271 fixed by lixu
    [self.view endEditing:YES];
    if ([self.group.GroupName isEqualToString:_textView.text]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        if (M_ISSTRNULL(_textView.text)) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            __weak EfengBeiZhuController *block = self;
            JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:nil message:@"您修改的信息是否需要保存?"];
            [alert addButton:Button_OK withTitle:@"是" handler:^(JKAlertDialogItem *item) {
                [block save];
            }];
            
            [alert addButton:Button_CANCEL withTitle:@"否" handler:^(JKAlertDialogItem *item) {
                
                [block.navigationController popViewControllerAnimated:YES ];
            }];
//            [alert show];
            [alert showInview:self.view];
        }
        
    }
}

//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    switch (buttonIndex) {
//        case 0:
//        {
//            [self.navigationController popViewControllerAnimated:YES ];
//        }
//            break;
//        case 1:
//        {
//            [self save];
//        }
//            break;
//        default:
//            break;
//    }
//}


#pragma mark 通知中心方法

-(void)updateSignature
{
    
    self.group.GroupName = _textView.text;
    [[CXmppRequestMgr shareInstance] updateDiscussInfoWithGroup:self.group];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
